<template>
    <div class="lazy-img">
        <div v-for="item in list" :key="item.id" class="lazy-img-box">
            <img v-lazyImg="'10%'" src="" alt="" :data-src="item.imgSrc" />
            <div class="lib-content">
                <div class="lib-title">{{ item.title }}</div>
                <div class="lib-direction">{{ item.content }}</div>
            </div>
        </div>
        <ScrollTop />
        <div class="lazy-img-count">{{ count }}</div>
    </div>
</template>
<script>
// import ScrollTop from "@/components/ScrollTop"
export default {
    name: 'LazyImg',
    components: {
      ScrollTop: () => import("@/components/ScrollTop")
    },
    data() {
        return {
            list: [],
            raf: null,
            count: 0,
        }
    },
    created() {
        const imgs = [
            'https://p.qqan.com/up/2022-8/2022815141509020.jpg',
            'https://p.qqan.com/up/2024-3/2024371341161115.jpg',
            'https://p.qqan.com/up/2024-3/2024381122484882.jpg',
            'https://p.qqan.com/up/2020-11/20201113147387125.png',
            'https://p.qqan.com/up/2024-3/17108273485873365.jpg',
            'https://p.qqan.com/up/2019-7/20197251828435752.jpg',
            'https://p.qqan.com/up/2020-8/202082410412170.png',
            'https://p.qqan.com/up/2024-3/17108273485165708.jpg',
            'https://p.qqan.com/up/2024-2/2024291618174085.png',
            'https://p.qqan.com/up/2020-5/20205111345315550.png',
            'https://p.qqan.com/up/2019-8/20198282036262885.png',
            'https://p.qqan.com/up/2019-8/20198131433419064.png',
            'https://p.qqan.com/up/2018-7/15312962547167280.jpg',
            'https://p.qqan.com/up/2024-3/2024371153272696.jpg',
            'https://p.qqan.com/up/2024-3/202436134098296.jpg',
        ]
        this.list = imgs.map((imgSrc, index) => {
            return {
                id: `img-${index}`,
                imgSrc,
                title: Math.floor(Math.random() * Math.pow(10, 6)).toString(16),
                content: Math.floor(Math.random() * Math.pow(10, 20)).toString(
                    16
                ),
            }
        })
    },
    mounted() {
        this.$nextTick(() => {
            window.mainRef.addEventListener('scroll', this.scorllWindow)
        })
    },
    methods: {
        scorllWindow(event) {
            if (!this.raf)
                this.raf = requestAnimationFrame(() => {
                    // let scrollTop = event.target.scrollTop.toString().split('.')[0]
                    // console.log('scorll', scrollTop)
                    // scrollTop = null
                    this.count = event.target.scrollTop.toString().split('.')[0]
                    this.raf = null
                })
        },
    },
    beforeDestroy() {
        window.mainRef.removeEventListener('scroll', this.scorllWindow)
    },
}
</script>
<style lang="scss" scoped>
.lazy-img {
    display: flex;
    flex-wrap: wrap;
    // flex-shrink: 2;
    row-gap: 40px;
    column-gap: calc(4% / 3);
    // justify-content: space-between;
    .lazy-img-box {
        width: calc(24% - 2px);
        height: 700px;
        border-radius: 10px;
        overflow: hidden;
        border: 1px solid gray;
        // box-shadow: 0 10px 10px rgba($color: #000000, $alpha: 0.5);
        transition: transform 0.5s ease, box-shadow 0.5s ease;
        &:hover {
            transform: translate(-2px, -2px);
            box-shadow: 0 10px 10px rgba($color: #000000, $alpha: 0.5);
        }
        &:nth-of-type(n + 5) {
            margin-top: 20px;
            border-color: red;
        }
        img {
            width: 100%;
            height: 600px;
        }
        .lib-content {
            .lib-title {
                font-size: 20px;
                font-weight: bold;
                text-shadow: 4px 4px 8px rgba($color: #000000, $alpha: 0.5);
            }
            .lib-direction {
                margin-top: 8px;
            }
        }
    }
    .lazy-img-count {
        position: fixed;
        top: 100px;
        right: 100px;
        width: 100px;
        height: 100px;
        padding: 40px;
        color: aliceblue;
        line-height: 100px;
        background-color: rgba(34, 34, 34, 0.4);
        border-radius: 10px;
        text-align: center;
        font-size: 26px;
    }
}
</style>
